package com.ruoyi.lvlian.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.lvlian.dto.LhsOrderItemDto;
import com.ruoyi.lvlian.vo.LhsOrderVo;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.lvlian.mapper.LhsOrderItemMapper;
import com.ruoyi.lvlian.domain.LhsOrderItem;
import com.ruoyi.lvlian.service.ILhsOrderItemService;

/**
 * 莱回收订单项Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Service
public class LhsOrderItemServiceImpl implements ILhsOrderItemService {
    @Autowired
    private LhsOrderItemMapper lhsOrderItemMapper;

    /**
     * 查询莱回收订单项
     *
     * @param id 莱回收订单项主键
     * @return 莱回收订单项
     */
    @Override
    public LhsOrderItem selectLhsOrderItemById(String id) {
        return lhsOrderItemMapper.selectLhsOrderItemById(id);
    }

    /**
     * 查询莱回收订单项列表
     *
     * @param lhsOrderItem 莱回收订单项
     * @return 莱回收订单项
     */
    @Override
    public List<LhsOrderItem> selectLhsOrderItemList(LhsOrderItemDto lhsOrderItem) {
        return lhsOrderItemMapper.selectLhsOrderItemList(lhsOrderItem);
    }

    /**
     * 新增莱回收订单项
     *
     * @param lhsOrderItem 莱回收订单项
     * @return 结果
     */
    @Override
    public LhsOrderItem insertLhsOrderItem(LhsOrderItem lhsOrderItem) {
        lhsOrderItem.setCreateUser(SecurityUtils.getUsername());
        lhsOrderItem.setCreateDate(DateUtils.getCurrentTime());


        lhsOrderItem.setId(MD5Utils.md5(lhsOrderItem.getOrderNum() + lhsOrderItem.getCategoryLevelName2()));
        lhsOrderItemMapper.insertLhsOrderItem(lhsOrderItem);
        return lhsOrderItem;
    }

    /**
     * 修改莱回收订单项
     *
     * @param lhsOrderItem 莱回收订单项
     * @return 结果
     */
    @Override
    public LhsOrderItem updateLhsOrderItem(LhsOrderItem lhsOrderItem) {

        LhsOrderItem updateResult = new LhsOrderItem();
        if (StringUtils.isEmpty(lhsOrderItem.getId())) {
            updateResult = insertLhsOrderItem(lhsOrderItem);
        } else {
            lhsOrderItemMapper.updateLhsOrderItem(lhsOrderItem);
        }
        updateResult = lhsOrderItemMapper.selectLhsOrderItemById(lhsOrderItem.getId());
        if (null == updateResult) {
            updateResult = insertLhsOrderItem(lhsOrderItem);
        }
        return updateResult;


    }

    /**
     * 批量删除莱回收订单项
     *
     * @param ids 需要删除的莱回收订单项主键
     * @return 结果
     */
    @Override
    public int deleteLhsOrderItemByIds(String[] ids) {
        return lhsOrderItemMapper.deleteLhsOrderItemByIds(ids);
    }

    /**
     * 删除莱回收订单项信息
     *
     * @param id 莱回收订单项主键
     * @return 结果
     */
    @Override
    public int deleteLhsOrderItemById(String id) {
        return lhsOrderItemMapper.deleteLhsOrderItemById(id);
    }

    @Override
    public  List<String>  batchInsertLhsOrderItem(List<LhsOrderItem> lhsOrderItemList) {
        List<String> ids = new ArrayList<>();
        if (CollectionUtils.isEmpty(lhsOrderItemList)) {
            return ids;
        }

        for (LhsOrderItem po : lhsOrderItemList) {
            po.setId(MD5Utils.md5(po.getOrderNum() + po.getCategoryLevelName2()));
            po.setCreateUser(SecurityUtils.getUsername());
            po.setCreateDate(DateUtils.getCurrentTime());
            ids.add(po.getId());
        }
        lhsOrderItemMapper.batchInsertLhsOrderItem(lhsOrderItemList);
        return ids;
    }


}
